window.main = window.main || {};

main.load = function(callback) {
	var callback = callback || qinglan.Function.emptyFun;
	qinglan.Canvas.World.globalRes.loadImg(qinglan.Canvas.Config.resource.img, function(img, percent, isload) {
		console.log(img.src + ' ' + percent + ' ' + isload);
		percent == 1 && callback();
	});
}

main.init = function() {
	console.log('OK');

	var fsm = qinglan.Canvas.World.fsmManager.getFsm('main');

	var enter = function(msg, fromstate) {
			qinglan.Debug.info('Enter!! state: [' + this.id + ']  msg: [' + msg + ']  fromstate: [' + fromstate + ']');
		};
	var leave = function() {
			qinglan.Debug.info('Leave!! state: [' + this.id + ']');
		};
	var meg = function(msg) {
			qinglan.Debug.info('Msg!! state: [' + this.id + ']  msg: [' + msg + ']');
		}
	var addTestAdvice = function(ids) {
			qinglan.Array.each(ids, function(id) {
				fsm.onEnter(id, 'enterTest', enter);
				fsm.onLeave(id, 'leaveTest', leave);
				fsm.onMessage(id, 'leaveTest', meg);
			})
		};
	addTestAdvice(['init', 'resourceLoad', 'mainMenu', 'modeSelect', 'play', 'over']);

	fsm.onEnter('init', 'initgame', function() {
		var bglayer = new qinglan.Canvas.Layer('bglayer', 0);
		var bgSprite = new qinglan.Canvas.Sprite('bg',new qinglan.Canvas.Vector2(0,0),{
			zIndex: 1,
			width : qinglan.Canvas.Config.frameWidth,
			height: qinglan.Canvas.Config.frameHeight
		});
		bgSprite.implement('draw', function(ctx){
			qinglan.Canvas.CanvasRender.drawFilledRect(ctx, 0, 0, this.width, this.height, qinglan.Canvas.Color.black);
		});
		bglayer.addSprite(bgSprite);
		qinglan.Canvas.World.frm.addLayer(bglayer);
		this.fsm.enter('resourceLoad', 'loadResource', false);
	});

	fsm.onEnter('resourceLoad', 'loadResource', function() {
		var self = this;
		main.load(function() {
			self.fsm.enter('mainMenu', 'enterMenu', false);
		})
	});

	fsm.onEnter('mainMenu', 'showMainMenu', function() {
		this.fsm.enter('play');
	});

	fsm.onEnter('play', 'particleTest', function() {
		main.particleTest();
	});

	fsm.enter('init');
};

main.init();

main.particleTest = function() {
	var barrageWhite = new qinglan.Canvas.Img('barrageWhite', new qinglan.Canvas.Vector2(100, 100), {
		imgNode: qinglan.Canvas.World.globalRes.getImgById('barrage.png'),
		offset: new qinglan.Canvas.Vector2(5, 211),
		view: new qinglan.Canvas.Vector2(25, 25)
	});

	var barrageRed = new qinglan.Canvas.Img('barrageRed', new qinglan.Canvas.Vector2(100, 100), {
		imgNode: qinglan.Canvas.World.globalRes.getImgById('barrage.png'),
		offset: new qinglan.Canvas.Vector2(34, 211),
		view: new qinglan.Canvas.Vector2(25, 25)
	});

	var barragePurple = new qinglan.Canvas.Img('barragePurple', new qinglan.Canvas.Vector2(100, 100), {
		imgNode: qinglan.Canvas.World.globalRes.getImgById('barrage.png'),
		offset: new qinglan.Canvas.Vector2(66, 211),
		view: new qinglan.Canvas.Vector2(25, 25)
	});

	var barrageBlue = new qinglan.Canvas.Img('barrageBlue', new qinglan.Canvas.Vector2(100, 100), {
		imgNode: qinglan.Canvas.World.globalRes.getImgById('barrage.png'),
		offset: new qinglan.Canvas.Vector2(97, 211),
		view: new qinglan.Canvas.Vector2(25, 25)
	});

	var bigC = new qinglan.Canvas.Img('bigC', null,{
		imgNode: qinglan.Canvas.World.globalRes.getImgById('barrage_boss.png'),
		translateCenter: true,
		offset: new qinglan.Canvas.Vector2(65, 191),
		view: new qinglan.Canvas.Vector2(65, 65)
	});

	var star = new qinglan.Canvas.Img('bigC', null,{
		imgNode: qinglan.Canvas.World.globalRes.getImgById('barrage_boss.png'),
		translateCenter: true,
		offset: new qinglan.Canvas.Vector2(96, 0),
		view: new qinglan.Canvas.Vector2(32, 32)
	});

	setTimeout(function(){
		qinglan.Canvas.World.frm.stop();
	},5000);
	var factory = new qinglan.Canvas.ParBulletFactory('test');

	factory.setOption({
		spellCard: {
			start: 0,
			end: 180,
			way: 10,
			num: 10,
			mode: qinglan.Canvas.ParSpread.line
		},
		trajectory: {
			from: {
				theta: 0,
				distance: 0
			},
			map:{
				theta: 10,
				distance: 800
			}
		},
		tween: {
			equal: qinglan.Tween.Quadratic.easeInOut,
			duration: 3000,
			delay: 300,
			type: 'to'
		},
		bullet: {
			//img: barrageWhite,
			//img: bigC,
			img: star,
			initPos: false,
			position: new qinglan.Canvas.Vector2(400,50)
		}
	});
	var spellCardWhite = factory.createSpellCard();
	

	factory.setOption({
		spellCard: {
			start: 0,
			end: 180,
			way: 10,
			num: 1,
			mode: qinglan.Canvas.ParSpread.assemble
		},
		trajectory: {
			from: {
				theta: 0,
				distance: 0
			},
			map:{
				theta: 90,
				distance: 500
			}
		},
		tween: {
			equal: qinglan.Tween.Quadratic.easeInOut,
			duration: 3000,
			delay: 350,
			type: 'to'
		},
		bullet: {
			//img: barrageRed,
			img: star,
			initPos: false,
			position: new qinglan.Canvas.Vector2(400,50)
		}
	});
	var spellCardRed = factory.createSpellCard();


	factory.setOption({
		spellCard: {
			start: 0,
			end: 180,
			way: 20,
			num: 15,
			mode: qinglan.Canvas.ParSpread.disperse
		},
		trajectory: {
			from: {
				theta: 0,
				distance: 0
			},
			map:{
				theta: 90,
				distance: 2000
			}
		},
		tween: {
			equal: qinglan.Tween.Quadratic.easeInOut,
			duration: 4000,
			delay: 200,
			type: 'to'
		},
		bullet: {
			img: barragePurple,
			initPos: false,
			position: new qinglan.Canvas.Vector2(400,50)
		}
	});
	var spellCardPurple = factory.createSpellCard();
	

	factory.setOption({
		spellCard: {
			start: 0,
			end: 180,
			way: 15,
			num: 15,
			mode: qinglan.Canvas.ParSpread.cross
		},
		trajectory: {
			from: {
				theta: 0,
				distance: 0
			},
			map:{
				theta: 90,
				distance: 800
			}
		},
		tween: {
			equal: qinglan.Tween.Quadratic.easeInOut,
			duration: 4000,
			delay: 250,
			type: 'to'
		},
		bullet: {
			img: barrageBlue,
			initPos: false,
			position: new qinglan.Canvas.Vector2(400,50)
		}
	});
	var spellCardBlue = factory.createSpellCard();

	//qinglan.Canvas.World.frm.getLayer('main').addSprite(spellCardWhite);
	qinglan.Canvas.World.frm.getLayer('main').addSprite(spellCardRed);
	//qinglan.Canvas.World.frm.getLayer('main').addSprite(spellCardPurple);
	//qinglan.Canvas.World.frm.getLayer('main').addSprite(spellCardBlue);

	qinglan.Canvas.World.frm.start();
	
};
